package com.eqs.enterprisequestionnaire.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.eqs.enterprisequestionnaire.mapper.AnswerMapper;
import com.eqs.enterprisequestionnaire.mapper.ResponseMapper;
import com.eqs.enterprisequestionnaire.mapper.UserMapper;
import com.eqs.enterprisequestionnaire.model.pojo.Answer;
import com.eqs.enterprisequestionnaire.model.pojo.Response;
import com.eqs.enterprisequestionnaire.model.pojo.User;
import com.eqs.enterprisequestionnaire.service.AnswerService;
import com.eqs.enterprisequestionnaire.service.ResponseService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AnswerServiceImpl extends ServiceImpl<AnswerMapper, Answer> implements AnswerService {

    @Resource
    AnswerMapper answerMapper;
    @Resource
    ResponseMapper responseMapper;
    @Resource
    UserMapper userMapper;
    /*
     * 查询所有
     * */
    public List<Answer> selectAll(Answer answer){

        Map<String ,Object> params =new HashMap<>();
        params.put("response_id",answer.getResponseId());
        params.put("question_id",answer.getQuestionId());
        params.put("content",answer.getContent());

        QueryWrapper<Answer> queryWrapper= new QueryWrapper<Answer>();

        queryWrapper.allEq( params,false);

        return list(queryWrapper);
    }

    /*
     * 根据问题ID获取该选项的所有答案
     * */
    public List<Answer> selectAllAnswerByQuestion(Integer question){
        Answer answer =new Answer();
        answer.setQuestionId(question);
        return selectAll(answer);
    }
    /*
     * 根据选项ID获取该选项的所有答案
     * */
    public List<Answer> selectAllAnswer(Integer optionId){
        Answer answer =new Answer();
        answer.setContent(optionId.toString());
        return selectAll(answer);
    }
    /*
    * 根据答案获取关联的用户
    * */
    public User selectUser(Answer answer){
        Response response =selectResponse(answer);
        return userMapper.selectById(response.getUserId());
    }
    /*
    * 根据答案获取关联的回答
    * */
    public Response selectResponse(Answer answer) {
        return responseMapper.selectById(answer.getResponseId());
    }
}
